<?php
if (!defined('BASEPATH'))
exit ('No direct script access allowed');

class Upmp {

	var $CI;


	public function __construct() {
		$this->CI = & get_instance();
	}

	public function pay($order_info){
		$this->CI->load->helper("upmp/upmp_service");
		//需要填入的部分
		$req['version']     		= upmp_config::$version; // 版本号
		$req['charset']     		= upmp_config::$charset; // 字符编码
		$req['transType']   		= "01"; // 交易类型
		$req['merId']       		= upmp_config::$mer_id; // 商户代码
		$req['backEndUrl']      	= $order_info['notify_url'] ; // 通知URL
		$req['frontEndUrl']     	= $order_info['return_url']; // 前台通知URL(可选)
		$req['orderDescription']	= $order_info['body'];// 订单描述(可选)
		$req['orderTime']   		= date("YmdHis",strtotime($order_info['orderTime'])); // 交易开始日期时间yyyyMMddHHmmss
		$req['orderTimeout']   		= ""; // 订单超时时间yyyyMMddHHmmss(可选)
		$req['orderNumber'] 		= $order_info['out_trade_no']; //订单号(商户根据自己需要生成订单号)
		$req['orderAmount'] 		= ($order_info['total_fee']*100); // 订单金额
		$req['orderCurrency'] 		= "156"; // 交易币种(可选)
		$req['reqReserved'] 		= $order_info['body']; // 请求方保留域(可选，用于透传商户信息)

		// 保留域填充方法
		$merReserved['test']   		= "test";
		$req['merReserved']   		= UpmpService::buildReserved($merReserved); // 商户保留域(可选)

		$resp = array ();
		$validResp = UpmpService::trade($req, $resp);
		log_message("info",http_build_query($resp));
		// 商户的业务逻辑
		if ($validResp){
			if ($resp['respCode']=="00") {
				$resp['mode']	=upmp_config::$mode;
				$this->CI->load->model("sws_recharge_order_model");
				$this->CI->sws_recharge_order_model->update_order_payment_order($order_info['out_trade_no'],$resp['tn']);
				$this->CI->load->view("pay/upmp_pay",$resp);
			}else{
				$data['error_info']="订单交易失败,请重新再试!";
				$this->CI->load->view("recharge/recharge_error",$data);
				return;
			}

		}else {
			$data['error_info']="交易失败,请重新再试!";
			$this->CI->load->view("recharge/recharge_error",$data);
			return;
		}
	}

	public function query($order_info){
		$this->CI->load->helper("upmp/upmp_service");
		//需要填入的部分
		$req['version']     	= upmp_config::$version; // 版本号
		$req['charset']     	= upmp_config::$charset; // 字符编码
		$req['transType']   	= "01"; // 交易类型
		$req['merId']       	= upmp_config::$mer_id; // 商户代码
		$req['orderTime']   	= date("YmdHis",strtotime($order_info['orderTime'])); // 交易开始日期时间yyyyMMddHHmmss或yyyyMMdd
		$req['orderNumber'] 	= $order_info['out_trade_no']; // 订单号


		// 保留域填充方法

		$merReserved['test']   	= "test";

		$req['merReserved']   	= UpmpService::buildReserved($merReserved); // 商户保留域(可选)

		$resp = array ();
		$validResp = UpmpService::query($req, $resp);

		// 商户的业务逻辑

		if ($validResp){

			return $resp;

		}else {

			// 服务器应答签名验证失败

			print_r($resp);

		}
	}

}